<?php
namespace app\admin\service;

use think\Loader;
use app\admin\service\Common;
use app\admin\model\Stock AS StockModel;
use app\admin\model\BalanceSheet AS BalanceSheetModel;

class BalanceSheet extends Common {

    const PROFIT_FORECASST_START = "https://datacenter.eastmoney.com/securities/api/data/get?type=RPT_F10_FINANCE_GBALANCE&sty=F10_FINANCE_GBALANCE&filter=";
    const PROFIT_FORECASST_END = "&p=1&ps=5&sr=-1&st=REPORT_DATE&source=HSF10&client=PC&v=";
    public function _initialize(){
    }

    public static function Collect($stockId = 0, $arr = []){

        $year = intval(date("Y"));
        $month = intval(date("m"));
        $day = intval(date("d"));
        $dataTime = '';
        if($month <= 12 && $month >= 9){
            $dataTime = $year."-06";
        }else if($month <= 9 && $month >= 6){
            $dataTime = $year."-03";
        }else if($month <= 3){
            $dataTime = ($year-1)."-12";
        }
        $dataTime .= "-30";
        $url = self::PROFIT_FORECASST_START.'('.urlencode('SECUCODE="'.$arr['stock_code'].'.'.$arr['jys'].'"').')';
        $url .= "(REPORT_DATE%20in%20(".urlencode("'".$dataTime."'").'))'.self::PROFIT_FORECASST_END;
        $reBalanceSheetJson = "";
        $reBalanceSheetJson = file_get_contents($url);
        if(!empty($reBalanceSheetJson)){
            $reBalanceSheetJson = json_decode($reBalanceSheetJson,true);
        }
        if(!empty($reBalanceSheetJson['result']['data'][0])){
            $reBalanceSheetJson = $reBalanceSheetJson['result']['data'][0];

            $data = [];
            //货币资金
            $data['monetary_capital'] = 0;
            if(!empty($reBalanceSheetJson['MONETARYFUNDS'])){
                $data['monetary_capital'] = $reBalanceSheetJson['MONETARYFUNDS'];
            }
            //拆出资金
            $data['open_funds'] = 0;
            if(!empty($reBalanceSheetJson['LEND_FUND'])){
                $data['open_funds'] = $reBalanceSheetJson['LEND_FUND'];
            }
            //拆入资金--------------------------------------
            $data['funding_borrowed'] = 0;

            //交易性金融资产
            $data['trad_financial_assets'] = 0;
            if(!empty($reBalanceSheetJson['TRADE_FINASSET_NOTFVTPL'])){
                $data['trad_financial_assets'] = floatval($reBalanceSheetJson['TRADE_FINASSET_NOTFVTPL']);
            }
            //衍生金融资产
            $data['derivative_financial_asset'] = 0;
            if(!empty($reBalanceSheetJson['DERIVE_FINASSET'])){
                $data['derivative_financial_asset'] = floatval($reBalanceSheetJson['DERIVE_FINASSET']);
            }
            //应收账款
            $data['accounts_receivable'] = 0;
            if(!empty($reBalanceSheetJson['ACCOUNTS_RECE'])){
                $data['accounts_receivable'] = floatval($reBalanceSheetJson['ACCOUNTS_RECE']);
            }
            //应收票据
            $data['notes_receivable'] = 0;
            if(!empty($reBalanceSheetJson['NOTE_ACCOUNTS_RECE'])){
                $data['notes_receivable'] = floatval($reBalanceSheetJson['NOTE_ACCOUNTS_RECE']);
            }
            //预收款项
            $data['advance_receipt'] = 0;
            if(!empty($reBalanceSheetJson['ADVANCE_RECEIVABLES'])){
                $data['advance_receipt'] = floatval($reBalanceSheetJson['ADVANCE_RECEIVABLES']);
            }
            //预付款项
            $data['prepayment'] = 0;
            if(!empty($reBalanceSheetJson['PREPAYMENT'])){
                $data['prepayment'] = floatval($reBalanceSheetJson['PREPAYMENT']);
            }
            //其他应收款合计
            $data['total_other_receivable'] = 0;
            if(!empty($reBalanceSheetJson['TOTAL_OTHER_RECE'])){
                $data['total_other_receivable'] = floatval($reBalanceSheetJson['TOTAL_OTHER_RECE']);
            }
            //应收股利------------------------------------------
            $data['dividends_receivable'] = 0;
            //融资应收款
            $data['financ_receivables'] = 0;
            //存货
            $data['save_stock'] = 0;
            if(!empty($reBalanceSheetJson['INVENTORY'])){
                $data['save_stock'] = floatval($reBalanceSheetJson['INVENTORY']);
            }
            //合同资产
            $data['contract_assets'] = 0;
            if(!empty($reBalanceSheetJson['CONTRACT_ASSET'])){
                $data['contract_assets'] = floatval($reBalanceSheetJson['CONTRACT_ASSET']);
            }
            //持有待售资产----------------------------------------------
            $data['hold_assets_sale'] = 0;
            //一年内到期的非流动资产
            $data['non_current_assets_matur_within_one_year'] = 0;
            if(!empty($reBalanceSheetJson['NONCURRENT_ASSET_1YEAR'])){
                $data['non_current_assets_matur_within_one_year'] = floatval($reBalanceSheetJson['NONCURRENT_ASSET_1YEAR']);
            }
            //其他流动资产
            $data['other_current_assets'] = 0;
            if(!empty($reBalanceSheetJson['OTHER_CURRENT_ASSET'])){
                $data['other_current_assets'] = floatval($reBalanceSheetJson['OTHER_CURRENT_ASSET']);
            }
            //流动资产合计
            $data['total_current_assets'] = 0;
            if(!empty($reBalanceSheetJson['TOTAL_CURRENT_ASSETS'])){
                $data['total_current_assets'] = floatval($reBalanceSheetJson['TOTAL_CURRENT_ASSETS']);
            }
            //债权投资
            $data['debt_investment'] = 0;
            if(!empty($reBalanceSheetJson['CREDITOR_INVEST'])){
                $data['debt_investment'] = floatval($reBalanceSheetJson['CREDITOR_INVEST']);
            }
            //长期应收款
            $data['long_term_receivable'] = 0;
            //长期股权投资
            $data['long_term_equity_investment'] = 0;
            if(!empty($reBalanceSheetJson['LONG_EQUITY_INVEST'])){
                $data['long_term_equity_investment'] = floatval($reBalanceSheetJson['LONG_EQUITY_INVEST']);
            }
            //其他权益工具投资
            $data['investments_other_equity_instrument'] = 0;
            if(!empty($reBalanceSheetJson['OTHER_EQUITY_INVEST'])){
                $data['investments_other_equity_instrument'] = floatval($reBalanceSheetJson['OTHER_EQUITY_INVEST']);
            }
            //其他非流动金融资产
            $data['other_non_current_financial_asset'] = 0;
            if(!empty($reBalanceSheetJson['OTHER_NONCURRENT_FINASSET'])){
                $data['other_non_current_financial_asset'] = floatval($reBalanceSheetJson['OTHER_NONCURRENT_FINASSET']);
            }
            //投资性房地产
            $data['investment_real_estate'] = 0;
            if(!empty($reBalanceSheetJson['INVEST_REALESTATE'])){
                $data['investment_real_estate'] = floatval($reBalanceSheetJson['INVEST_REALESTATE']);
            }
            //固定资产
            $data['fixed_asset'] = 0;
            if(!empty($reBalanceSheetJson['FIXED_ASSET'])){
                $data['fixed_asset'] = floatval($reBalanceSheetJson['FIXED_ASSET']);
            }
            //在建工程
            $data['construction_progre'] = 0;
            if(!empty($reBalanceSheetJson['CIP'])){
                $data['construction_progre'] = floatval($reBalanceSheetJson['CIP']);
            }
            //在建工程
            $data['construction_progre'] = 0;
            if(!empty($reBalanceSheetJson['CIP'])){
                $data['construction_progre'] = floatval($reBalanceSheetJson['CIP']);
            }
            //使用权资产
            $data['usufruct_asset'] = 0;
            if(!empty($reBalanceSheetJson['USERIGHT_ASSET'])){
                $data['usufruct_asset'] = floatval($reBalanceSheetJson['USERIGHT_ASSET']);
            }
            //无形资产
            $data['intangible_asset'] = 0;
            if(!empty($reBalanceSheetJson['INTANGIBLE_ASSET'])){
                $data['intangible_asset'] = floatval($reBalanceSheetJson['INTANGIBLE_ASSET']);
            }
            //商誉
            $data['stock_goodwill'] = 0;
            if(!empty($reBalanceSheetJson['GOODWILL'])){
                $data['stock_goodwill'] = floatval($reBalanceSheetJson['GOODWILL']);
            }
            //长期待摊费用
            $data['long_term_deferred_expense'] = 0;
            if(!empty($reBalanceSheetJson['LONG_PREPAID_EXPENSE'])){
                $data['long_term_deferred_expense'] = floatval($reBalanceSheetJson['LONG_PREPAID_EXPENSE']);
            }
            //递延所得税资产
            $data['deferred_tax_asset'] = 0;
            if(!empty($reBalanceSheetJson['DEFER_TAX_ASSET'])){
                $data['deferred_tax_asset'] = floatval($reBalanceSheetJson['DEFER_TAX_ASSET']);
            }
            //其他非流动资产
            $data['other_non_current_asset'] = 0;
            if(!empty($reBalanceSheetJson['OTHER_NONCURRENT_ASSET'])){
                $data['other_non_current_asset'] = floatval($reBalanceSheetJson['OTHER_NONCURRENT_ASSET']);
            }
            //非流动资产合计
            $data['total_non_current_asset'] = 0;
            if(!empty($reBalanceSheetJson['TOTAL_NONCURRENT_ASSETS'])){
                $data['total_non_current_asset'] = floatval($reBalanceSheetJson['TOTAL_NONCURRENT_ASSETS']);
            }
            //资产总计
            $data['total_asset'] = 0;
            if(!empty($reBalanceSheetJson['TOTAL_ASSETS'])){
                $data['total_asset'] = floatval($reBalanceSheetJson['TOTAL_ASSETS']);
            }
            //短期借款
            $data['note_payable'] = 0;
            if(!empty($reBalanceSheetJson['TOTAL_ASSETS'])){
                $data['note_payable'] = floatval($reBalanceSheetJson['TOTAL_ASSETS']);
            }
            //应付票据
            $data['short_term_loan'] = 0;
            if(!empty($reBalanceSheetJson['NOTE_PAYABLE'])){
                $data['short_term_loan'] = floatval($reBalanceSheetJson['NOTE_PAYABLE']);
            }
            //应付账款
            $data['account_payable'] = 0;
            if(!empty($reBalanceSheetJson['ACCOUNTS_PAYABLE'])){
                $data['account_payable'] = floatval($reBalanceSheetJson['ACCOUNTS_PAYABLE']);
            }
            //预收款项
            $data['advance_received'] = 0;
            if(!empty($reBalanceSheetJson['ADVANCE_RECEIVABLES'])){
                $data['advance_received'] = floatval($reBalanceSheetJson['ADVANCE_RECEIVABLES']);
            }
            //合同负债
            $data['contractual_liability'] = 0;
            if(!empty($reBalanceSheetJson['CONTRACT_LIAB'])){
                $data['contractual_liability'] = floatval($reBalanceSheetJson['CONTRACT_LIAB']);
            }
            //应付职工薪酬
            $data['contractual_liability'] = 0;
            if(!empty($reBalanceSheetJson['STAFF_SALARY_PAYABLE'])){
                $data['contractual_liability'] = floatval($reBalanceSheetJson['STAFF_SALARY_PAYABLE']);
            }
            //应交税费
            $data['taxe_payable'] = 0;
            if(!empty($reBalanceSheetJson['TAX_PAYABLE'])){
                $data['taxe_payable'] = floatval($reBalanceSheetJson['TAX_PAYABLE']);
            }
            //其他应付款合计
            $data['taxe_payable'] = 0;
            if(!empty($reBalanceSheetJson['TOTAL_OTHER_PAYABLE'])){
                $data['taxe_payable'] = floatval($reBalanceSheetJson['TOTAL_OTHER_PAYABLE']);
            }
            //应付股利
            $data['dividend_payable'] = 0;
            if(!empty($reBalanceSheetJson['DIVIDEND_PAYABLE'])){
                $data['dividend_payable'] = floatval($reBalanceSheetJson['DIVIDEND_PAYABLE']);
            }
            //一年内到期的非流动负债
            $data['non_current_liabilitie_due_within_one_year'] = 0;
            if(!empty($reBalanceSheetJson['NONCURRENT_LIAB_1YEAR'])){
                $data['non_current_liabilitie_due_within_one_year'] = floatval($reBalanceSheetJson['NONCURRENT_LIAB_1YEAR']);
            }
            //其他流动负债
            $data['other_current_liabilitie'] = 0;
            if(!empty($reBalanceSheetJson['OTHER_CURRENT_LIAB'])){
                $data['other_current_liabilitie'] = floatval($reBalanceSheetJson['OTHER_CURRENT_LIAB']);
            }
            //流动负债合计
            $data['total_current_liabilitie'] = 0;
            if(!empty($reBalanceSheetJson['TOTAL_CURRENT_LIAB'])){
                $data['total_current_liabilitie'] = floatval($reBalanceSheetJson['TOTAL_CURRENT_LIAB']);
            }
            //长期借款
            $data['long_term_loan'] = 0;
            if(!empty($reBalanceSheetJson['LONG_LOAN'])){
                $data['long_term_loan'] = floatval($reBalanceSheetJson['LONG_LOAN']);
            }
            //应付债券
            $data['bond_payable'] = 0;
            if(!empty($reBalanceSheetJson['TOTAL_OTHER_RECE'])){
                $data['bond_payable'] = floatval($reBalanceSheetJson['TOTAL_OTHER_RECE']);
            }
            //租赁负债
            $data['lease_liability'] = 0;
            if(!empty($reBalanceSheetJson['LEASE_LIAB'])){
                $data['lease_liability'] = floatval($reBalanceSheetJson['LEASE_LIAB']);
            }
            //长期应付款
            $data['long_term_payable'] = 0;
            if(!empty($reBalanceSheetJson['LONG_PAYABLE'])){
                $data['long_term_payable'] = floatval($reBalanceSheetJson['LONG_PAYABLE']);
            }
            //预计负债
            $data['projected_liabilitie'] = 0;
            if(!empty($reBalanceSheetJson['PREDICT_LIAB'])){
                $data['projected_liabilitie'] = floatval($reBalanceSheetJson['PREDICT_LIAB']);
            }
            //递延收益
            $data['deferred_income'] = 0;
            if(!empty($reBalanceSheetJson['DEFER_INCOME'])){
                $data['deferred_income'] = floatval($reBalanceSheetJson['DEFER_INCOME']);
            }
            //递延所得税负债
            $data['deferred_income_tax_liabilitie'] = 0;
            if(!empty($reBalanceSheetJson['DEFER_TAX_LIAB'])){
                $data['deferred_income_tax_liabilitie'] = floatval($reBalanceSheetJson['DEFER_TAX_LIAB']);
            }
            //非流动负债合计
            $data['total_non_current_liabilitie'] = 0;
            if(!empty($reBalanceSheetJson['TOTAL_NONCURRENT_LIAB'])){
                $data['total_non_current_liabilitie'] = floatval($reBalanceSheetJson['TOTAL_NONCURRENT_LIAB']);
            }
            //负债合计
            $data['total_liabilitie'] = 0;
            if(!empty($reBalanceSheetJson['TOTAL_LIABILITIES'])){
                $data['total_liabilitie'] = floatval($reBalanceSheetJson['TOTAL_LIABILITIES']);
            }
            //实收资本（或股本）
            $data['paid_capital'] = 0;
            if(!empty($reBalanceSheetJson['SHARE_CAPITAL'])){
                $data['paid_capital'] = floatval($reBalanceSheetJson['SHARE_CAPITAL']);
            }
            //资本公积
            $data['capital_reserve'] = 0;
            if(!empty($reBalanceSheetJson['CAPITAL_RESERVE'])){
                $data['capital_reserve'] = floatval($reBalanceSheetJson['CAPITAL_RESERVE']);
            }
            //库存股
            $data['treasury_stock'] = 0;
            if(!empty($reBalanceSheetJson['TREASURY_SHARES'])){
                $data['treasury_stock'] = floatval($reBalanceSheetJson['TREASURY_SHARES']);
            }
            //其他综合收益
            $data['other_comprehensive_income'] = 0;
            if(!empty($reBalanceSheetJson['OTHER_COMPRE_INCOME'])){
                $data['other_comprehensive_income'] = floatval($reBalanceSheetJson['OTHER_COMPRE_INCOME']);
            }
            //盈余公积
            $data['surplu_reserve'] = 0;
            if(!empty($reBalanceSheetJson['SURPLUS_RESERVE'])){
                $data['surplu_reserve'] = floatval($reBalanceSheetJson['SURPLUS_RESERVE']);
            }
            //未分配利润
            $data['undistributed_profit'] = 0;
            if(!empty($reBalanceSheetJson['UNASSIGN_RPOFIT'])){
                $data['undistributed_profit'] = floatval($reBalanceSheetJson['UNASSIGN_RPOFIT']);
            }
            //归属于母公司股东权益总计
            $data['total_equity_attributable_shareholders_parent_company'] = 0;
            if(!empty($reBalanceSheetJson['TOTAL_PARENT_EQUITY'])){
                $data['total_equity_attributable_shareholders_parent_company'] = floatval($reBalanceSheetJson['TOTAL_PARENT_EQUITY']);
            }
            //少数股东权益
            $data['minority_interest'] = 0;
            if(!empty($reBalanceSheetJson['MINORITY_EQUITY'])){
                $data['minority_interest'] = floatval($reBalanceSheetJson['MINORITY_EQUITY']);
            }
            //股东权益合计
            $data['total_stockholder_equity'] = 0;
            if(!empty($reBalanceSheetJson['TOTAL_EQUITY'])){
                $data['total_stockholder_equity'] = floatval($reBalanceSheetJson['TOTAL_EQUITY']);
            }
            //负债和股东权益总计
            $data['total_liabilitie_shareholder_equity'] = 0;
            if(!empty($reBalanceSheetJson['TOTAL_LIAB_EQUITY'])){
                $data['total_liabilitie_shareholder_equity'] = floatval($reBalanceSheetJson['TOTAL_LIAB_EQUITY']);
            }
            $data['remark'] = json_encode($reBalanceSheetJson);
            $where = ['sid'=>$stockId];
            if(BalanceSheetModel::Detail($where)){
                BalanceSheetModel::where($where)->update($data);
            }else{
                $data['sid'] = $stockId;
                BalanceSheetModel::insert($data);
            }
        }
        return 1;
    }
}